﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Text;
using Dos.Common;


namespace wxkq.Controllers
{
    public class CommonController : Controller
    {
        protected override JsonResult Json(object data, string contentType,Encoding contentEncoding, JsonRequestBehavior behavior) {
            if (behavior == JsonRequestBehavior.DenyGet && string.Equals(this.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase)) {
                return new JsonResult();

            }

            return new JsonNetResult()
            {
                Data = data,
                ContentType = contentType,
                ContentEncoding = contentEncoding
            };
        }
    }

    public class JsonNetResult : JsonResult {
        public override void ExecuteResult(ControllerContext context)
        {
            if (context == null) {
                throw new ArgumentNullException("context");
            }

            var response = context.HttpContext.Response;
            response.ContentType = !string.IsNullOrEmpty(ContentType) ? ContentType : "application/json";
            if (ContentEncoding != null) {
                response.ContentEncoding = ContentEncoding;
            }
            if (Data != null) {
                //Newtonsoft.Json.Converters.IsoDateTimeConverter timeConverter = new Newtonsoft.Json.Converters.IsoDateTimeConverter();
                //timeConverter.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
                //string json = Newtonsoft.Json.JsonConvert.SerializeObject(Data, timeConverter); //把对象集合转换成Json


                string json = JSON.ToJSON(Data, new JSONParameters()
                {
                    DateFormatString = "yyyy-MM-dd HH:mm:ss"
                });
                //string json = JSON.ToJSON(Data);

                response.Write(json);
            }

        }
    }
}